package gl.android.biyiqing.fragment;

import gl.android.biyiqing.R;
import gl.android.biyiqing.api.API;
import gl.android.biyiqing.competent.PayView;
import gl.common.AjaxCallBackProxy;
import gl.common.BaseControl;
import gl.common.UserManger;
import gl.uitls.AndroidUtil;
import gl.uitls.Tip;

import org.json.JSONObject;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class VipUpdateFragment<infos> extends BaseFragment implements
		android.view.View.OnClickListener {

	// Content View Elements

	public RelativeLayout pay;
	public TextView tv_vip_group;
	public TextView tv_vip_name;
	public RelativeLayout hy1;
	public TextView youxiaoqi1;
	public Button btn_buy1;
	public RelativeLayout hy2;
	public TextView youxiaoqi2;
	public Button btn_buy2;
	public RelativeLayout hy3;
	public TextView youxiaoqi3;
	public Button btn_buy3;
	public TextView tv1;
	public TextView tv2;
	public TextView tv3;

	// End Of Content View Elements

	private void bindViews() {

		pay = (RelativeLayout) findViewById(R.id.pay);
		tv_vip_group = (TextView) findViewById(R.id.tv_vip_group);
		tv_vip_name = (TextView) findViewById(R.id.tv_vip_name);
		hy1 = (RelativeLayout) findViewById(R.id.hy1);
		youxiaoqi1 = (TextView) findViewById(R.id.youxiaoqi1);
		btn_buy1 = (Button) findViewById(R.id.btn_buy1);
		hy2 = (RelativeLayout) findViewById(R.id.hy2);
		youxiaoqi2 = (TextView) findViewById(R.id.youxiaoqi2);
		btn_buy2 = (Button) findViewById(R.id.btn_buy2);
		hy3 = (RelativeLayout) findViewById(R.id.hy3);
		youxiaoqi3 = (TextView) findViewById(R.id.youxiaoqi3);
		btn_buy3 = (Button) findViewById(R.id.btn_buy3);
		tv1 = (TextView) findViewById(R.id.tv1);
		tv2 = (TextView) findViewById(R.id.tv2);
		tv3 = (TextView) findViewById(R.id.tv3);
		btn_buy1.setOnClickListener(this);
		btn_buy2.setOnClickListener(this);
		btn_buy3.setOnClickListener(this);
	}

	class BuyOnClickIMP implements View.OnClickListener {
		private Object[] pars;
		private String url;

		BuyOnClickIMP(String url, Object[] pars) {
			this.url = url;
			this.pars = pars;
		}

		@Override
		public void onClick(View v) {
			buy(url, pars);
		}
	}

	public static BaseFragment newInstance() {
		VipUpdateFragment fragment = new VipUpdateFragment();
		Bundle args = new Bundle();
		fragment.setArguments(args);
		return fragment;
	}

	public VipUpdateFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.my_vip_update, container, false);
		type = Integer.parseInt(getArguments().getString(BaseFragment.KEY_SEX));
		bindViews();
		initData();
		return rootView;
	}

	private void initData() {
		initCountInfo();
	}

	String ERR = "购买失败";

	public void buy(String url, Object[] pars) {
		BaseControl.get(API.URL + url, new AjaxCallBackProxy() {

			@Override
			public void onSuccess(String t) {
				super.onSuccess(t);
				try {
					handErrMsg(t);
					final JSONObject jsonObject = new JSONObject(t);
					if ("3".equals(jsonObject.getString("msg"))) {
						Tip.show("购买成功");
						initCountInfo();
					}
					else if ("0".equals(jsonObject.getString("msg"))) {
						Tip.show("金币不足");
					}
				} catch (Exception e) {
					e.printStackTrace();
					onFailure(e, 0, e.getMessage());
				}
			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				Tip.show(ERR);
			}
		}, pars);
	}

	private int type;

	private Object[] infos;
	int price = 30;
	int vip_level_current = 1;
	int vip_level_update ;
	public void initCountInfo() {
		getCount("GetMoneyCount?userid=%s", tv_vip_group, "拥有金币:%s");
		
		vip_level_update = type+1;
		// 20 30 50
		switch (type) {
		case 1:
			price = 30;
			infos = new Object[] { "高级", price * 1, price * 3, price * 6, 20,
					R.drawable.ic_cuo, R.drawable.ic_cuo };
			break;
		case 2:
			price = 60;
			infos = new Object[] { "VIP", price * 1, price * 3, price * 6, 30,
					R.drawable.ic_dui, R.drawable.ic_dui };
			break;
		case 3:
			price = 90;
			infos = new Object[] { "钻石", price * 1, price * 3, price * 6, 50,
					R.drawable.ic_dui, R.drawable.ic_dui };
			break;
		}

		tv_vip_name.setText(String.format(tv_vip_name.getText().toString(),
				infos[0]));
		youxiaoqi1.setText(String.format(youxiaoqi1.getText().toString(),
				infos[1]));
		youxiaoqi2.setText(String.format(youxiaoqi2.getText().toString(),
				infos[2]));
		youxiaoqi3.setText(String.format(youxiaoqi3.getText().toString(),
				infos[3]));
		tv1.setText(String.format(tv1.getText().toString(), infos[4]));
		// FIXME 特权 图片不限
		tv2.setCompoundDrawables(null, null,
				getResources().getDrawable((Integer) infos[5]), null);
		tv3.setCompoundDrawables(null, null,
				getResources().getDrawable((Integer) infos[6]), null);
	}

	void getCount(String url, final TextView tv, final String format) {
		BaseControl.get(API.URL + url, new AjaxCallBackProxy() {
			@Override
			public void onSuccess(String t) {
				super.onSuccess(t);
				try {
					tv.setText(String.format(format,
							new JSONObject(t).getString("data")));

				} catch (Exception e) {
					e.printStackTrace();
					onFailure(e, 0, e.getMessage());
				}
			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				Tip.show("获取拥有财产数量失败");
			}
		}, new Object[] { UserManger.getInstance().userid });
	}

	private void handErrMsg(String t) {
	}

	AlertDialog _Dialog;

	public void onClickBuyVIP(final String url) {
		final EditText et = new EditText(getActivity());

		et.setInputType(EditorInfo.TYPE_CLASS_NUMBER);

		View PayView = new PayView(getActivity(), new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				_Dialog.dismiss();
				try {

					String[] pars = new String[] {
							UserManger.getInstance().userid,
							v.getTag().toString() };
					buy(url, pars);
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		}).getView();
		_Dialog = AndroidUtil.genCustomViewDialog(getActivity(), PayView)
				.show();
	}

	private String url = "BuyUserVIP?userid=%s&am=%s&days=%s&currentlvl=%s&updatelvl=%s";
	@Override
	public void onClick(View v) {
		if (v == btn_buy1) {

			final Object[] args = new Object[] {
					UserManger.getInstance().userid, price * 1, 30 * 1,
					vip_level_current, vip_level_update };
			url = String.format(url, args);
			buy(url, args);
		}
		else if (v == btn_buy2) {
			final Object[] args = new Object[] {
					UserManger.getInstance().userid, price * 3, 30 * 3,
					vip_level_current, vip_level_update };
			url = String.format(url, args);
			buy(url, args);
		}
		else if (v == btn_buy3) {
			final Object[] args = new Object[] {
					UserManger.getInstance().userid, price * 6, 30 * 6,
					vip_level_current, vip_level_update };
			url = String.format(url, args);
			buy(url, args);
		}

	}

}
